package com.css.fxfzcpxx.modules.typeManage.repository.impl;

import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.courseManage.entity.CourseManage;
import com.css.fxfzcpxx.modules.informationAchievement.entity.InfAchievement;
import com.css.fxfzcpxx.modules.informationDelivery.entity.InfDelivery;
import com.css.fxfzcpxx.modules.questionData.entity.QuestionData;
import com.css.fxfzcpxx.modules.typeManage.entity.TypeManage;
import com.css.fxfzcpxx.modules.typeManage.param.TypeManageParam;
import com.css.fxfzcpxx.modules.typeManage.repository.TypeManageRepository;
import com.css.fxfzcpxx.modules.typeManage.vo.TypeManageVO;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/08
 * @Description: 类别管理
 */
@Repository
public class TypeManageRepositoryImpl implements TypeManageRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "cpxx_type_manage";

    private static final String TABLE_ID = "id"; // 表id字段名

    @Override
    public List<TypeManage> getType(String plate) {
        String sql = "select * from "+TABLE_NAME+" where del_flag = '0' and type_plate = '"+plate+"' order by create_time desc";
        List<TypeManage> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TypeManage.class));
        return list;
    }

    @Override
    public void save(TypeManage typeManage) {
        String sql = "insert into "+TABLE_NAME+"(id,type_plate,type_name,type_desc,del_flag,create_user,create_time) " +
                    "values(:id, :typePlate, :typeName, :typeDesc, :delFlag, :createUser, :createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(typeManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delete(String id) {
        String sql = "update "+TABLE_NAME+" set del_flag = '"+ YNEnum.Y.toString()+"' where id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void update(TypeManage typeManage) {
        String sql = "update "+TABLE_NAME+" set type_name= :typeName ,type_desc = :typeDesc ," +
                "update_user = :updateUser ,update_time = :updateTime where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(typeManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public TypeManage queryIdByName(String name) {
        String sql ="select * from " + TABLE_NAME+"  where del_flag = '0' and type_plate = '题库' and type_name = '"+name+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TypeManage.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<InfDelivery> queryDataDelivery(String id) {
        String sql ="select * from cpxx_information_delivery  where del_flag = '0' and means_type_id = '"+id+"'";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(InfDelivery.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<CourseManage> queryDataCourse(String id) {
        String sql ="select * from cpxx_course_manage  where del_flag = '0' and course_type_id = '"+id+"'";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CourseManage.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<QuestionData> queryDataQuestion(String id) {
        String sql ="select * from cpxx_question_data  where del_flag = '0' and type_id = '"+id+"'";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(QuestionData.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> getPage(TypeManageParam param, String tableNameSql,String name ,String typeId,List<String> asList) {
        StringBuilder sql = new StringBuilder(tableNameSql);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = 0;
        int lastIndex = 500;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //名称
        if (!PlatformObjectUtils.isEmpty(param.getName())) {
            whereSql.append(" and "+name+" like '%" + param.getName() + "%'");
        }
        //类别
        if (!PlatformObjectUtils.isEmpty(param.getTypeId())) {
            whereSql.append(" and "+typeId+" in  ('" + String.join("','", asList) + "')");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(TypeManageVO.class)).size();
        List<TypeManageVO> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(TypeManageVO.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void transferByIds(String tableName,String type, String typeId, List<String> asList) {
        String sql="update  "+tableName+" set "+type+" = '"+typeId+"' where id in  ('" + String.join("','", asList) + "')";
        jdbcTemplate.update(sql);
    }

    @Override
    public void transferByIdsALm(String lm, String type, String typeId, List<String> asList) {
        String sql="update  cpxx_information_delivery set "+type+" = '"+typeId+"' , release_column = '"+lm+"' where id in  ('" + String.join("','", asList) + "')";
        jdbcTemplate.update(sql);
    }

    @Override
    public void transferByTypeId(String tableName, String type, String newTypeId, List<String> asList) {
        String sql="update  "+tableName+" set "+type+" = '"+newTypeId+"' where "+type+" in  ('" + String.join("','", asList) + "')";
        jdbcTemplate.update(sql);
    }

    @Override
    public void transferByTypeIdALm(String lm, String type, String newTypeId, List<String> asList) {
        String sql="update  cpxx_information_delivery set "+type+" = '"+newTypeId+"' , release_column = '"+lm+"' where "+type+" in  ('" + String.join("','", asList) + "')";
        jdbcTemplate.update(sql);
    }

    @Override
    public TypeManage queryById(String id) {
        String sql ="select * from " + TABLE_NAME+" where id = '"+id+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TypeManage.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public TypeManage queryByPlateAndName(String typePlate, String typeName) {
//        String sql ="select * from " + TABLE_NAME+" where del_flag = '0' and type_plate = '"+typePlate+"' and type_name = '"+typeName+"'";
//        try {
//            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TypeManage.class));
//        } catch (EmptyResultDataAccessException e) {
//            return null;
//        }
        String sql="select * from " + TABLE_NAME+" where del_flag = '0' and type_plate = :typePlate and type_name = :typeName";
        Map<String,String> idMap = new HashMap();
        idMap.put("typePlate", typePlate);
        idMap.put("typeName", typeName);
        try {
            return namedParameterJdbcTemplate.queryForObject(sql, idMap, new BeanPropertyRowMapper<>(TypeManage.class));
        }catch (Exception exception){
            return null;
        }
    }
}
